<!-- // created by: alfin akhret
// files dependency:
// - region_cluster.js
// - cluster.php (controller)
// - mcluster.php (model)
// - f-cluster.php (view)
// - smooth.php (template) -->
<div class="box">
        <!-- box / title -->
        <?php
            // date_default_timezone_set('Asia/Jakarta');
            // echo date('H:i:s');die();
        ?>
        <div class="title">
                <h1>Cluster</h1>
                <ul class="links">
                        <!-- <li><a href="<?=base_url(); ?>index.php/handset/newHandset">Handset Baru</a></li>
                        <li><a href="<?=base_url(); ?>index.php/handset">Data Handset</a></li> -->
                </ul>
        </div>

<?php 
  ## Display the error message here
  $message = get_instance()->session->flashdata('message');
  if ($message) : $div_class = get_instance()->session->flashdata('div_class');
?>
  <div class="<?php $div_class; ?>">
    <?php echo $message; ?>
  </div>
<?php endif; ## End of error messages ?>

        <!-- end box / title -->
                <form id="form" action="" method="post" class="form-vertical">
                <?php if($logic == '1') { ?>
                        <div class="form">
                                <div class="fields">
                                        <div class="field">
                                                <div class="label">
                                                        <label for="region">Region:</label>
                                                </div>
                                                <div class="input">
                                                        <span><?php echo $region_name; ?></span>
                                                </div>
                                        </div>
                                        <div class="field">
                                                <div class="label">
                                                        <label for="cluster">Cluster:</label>
                                                </div>
                                                <div class="input">
                                                        <span><?php echo $cluster_name; ?></span>
                                                </div>
                                        </div>
                                </div>
                        </div>
                <?php 
                } else if($logic == '2') { ?>
                         <div class="form">
                                <div class="fields">
                                        <div class="field">
                                                <div class="label">
                                                        <label for="region">Region:</label>
                                                </div>
                                                <div class="input">
                                                        <input type="hidden" name="region_id" value="<?php echo $region_id;?>">
                                                        <span><?php echo strtoupper($region_name); ?></span>
                                                </div>
                                        </div>
                                        <div class="field">
                                                <div class="label">
                                                        <label for="cluster">Cluster:</label>
                                                </div>
                                                <div class="input">
                                                        <select id="cluster" name="cluster_id" class="medium">
                                                                <?php
                                                                        foreach ($cluster_array as $r) {
                                                                                echo '<option value="'.$r->mvalue.'">'.$r->mdisplay.'</option>';
                                                                        }
                                                                ?>
                                                        </select>
                                                </div>
                                        </div>
                                       
                                        <div class="buttons">
                                                <input type="submit" name="submit" value="Submit" id="submit"/>
                                                <!-- <input type="reset" name="reset" value="Reset" /> -->
                                        </div>
                                </div>
                        </div>
                <?php 
                } else {
                ?>
                        <div class="form">
                                <div class="fields">
                                        <div class="field">
                                                
												<div class="control-group">
													<label class="control-label" for="region">Region:</label>
													<div class="controls controls-row">
														<select id="region" name="region_id" class="medium">
																<option value="">Pilih region</option>
																<?php
																		foreach ($region_array as $r) {
																				echo '<option value="'.$r->territory_id.'">'.strtoupper($r->territory_name).'</option>';
																		}
																?>
														</select>
													</div>
												</div>
                                        </div>
                                        <div class="field">
                                            <div class="control-group">
												<label class="control-label" for="cluster">Cluster:</label>
                                                <div class="controls controls-row">
													<select id="cluster" name="cluster_id" class="medium" disabled>
															<?php
															if ($cluster_array) {
																	foreach ($cluster_array as $r) {
																			echo '<option value="'.$r->territory_id.'">'.$r->territory_name.'</option>';
																	}}
															?>
													</select>
                                                </div>
											</div>
                                        </div>
                                       
                                        <div class="buttons">
                                                <input type="submit" name="submit" value="Submit" id="submit" class="btn btn-primary" />
                                                <!-- <input type="reset" name="reset" value="Reset" /> -->
                                        </div>
                                </div>
                        </div>

                <? } ?>
                </form><br><br>&nbsp;
       
        
        <?php
        $msg = get_instance()->session->flashdata('message');
        if($msg <> ""){
              echo'<div id="box-messages">
                        <div class="messages">
                                <div id="message-success" class="message message-success">
                                        <div class="image">
                                                <img src="'. base_url() .'file/shell/smooth/resources/images/icons/success.png" alt="Success" height="32" />
                                        </div>
                                        <div class="text">
                                                <h6>'. $msg .'</h6>
                                        </div>
                                        <div class="dismiss">
                                                <a href="#message-success"></a>
                                        </div>
                                </div>
                        </div>
                </div>';
        }
        ?>

</div>

<script><!--
$(function() {

	$('#region').change(function(){
		var region = $(this).val()
		if (region == "" ) {
			alert('Silakan pilih Region');
		}
		else {	
			$.ajax({
				url: '<?php echo base_url('/territory/cluster/3') ?>' + '/' + region, //"cluster/get_cluster/3/" + region,
				data: { cmd: 'GET_CLUSTER' },
				dataType:"json",
				type: "get",
				success: function(data){
					$('#cluster option').remove();
					$.each(data, function(i, n) {
					    $('#cluster').append(
					        $('<option></option>').val(n['mvalue']).html(n['mdisplay'])
					    );
					});
					$('#cluster').removeAttr('disabled');
				}
			});
		}
	});

});

document.getElementsByName('submit')[0].onclick = function() {
	if (document.getElementById('region').value == '') {
		alert('Silakan pilih Region');
		document.getElementById('region').focus();
		return false;
	}
};
//--></script>